package com.lc.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lc.service.ApiService;
import com.lc.util.BeanUtils;
import com.lc.util.HttpRequestHelper;
import com.lc.util.MD5;
import com.lc.util.YyghException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@Service
@Slf4j
public class ApiServiceImpl implements ApiService{
//    public JSONObject getHospital() {
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("hoscode",this.getHoscode());
//        paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
//        paramMap.put("sign", MD5.encrypt(this.getSignKey()));
//        JSONObject respone = HttpRequestHelper.sendRequest(paramMap,this.getApiUrl()+"/api/hosp/hospital/show");
//        System.out.println(respone.toJSONString());
//        if(null != respone && 20000== respone.getIntValue("code")) {
//            JSONObject jsonObject = respone.getJSONObject("data");
//            return jsonObject;
//        }
//        return null;
//    }

    @Override
    public JSONObject getOffice() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("officecode","10000_7");
        JSONObject response= HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/show");
        if (null!=response&&20000== response.getIntValue("code"))
        {
            return response.getJSONObject("data");
        }
        return null;
    }

    @Override
    public boolean saveOffice(String data) {
        JSONObject jsonObject=JSONObject.parseObject(data);
       //System.out.println(jsonObject.toString());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("officecode",jsonObject.getString("officecode"));
        paramMap.put("officename",jsonObject.getString("officename"));
        paramMap.put("provinceCode",jsonObject.getString("provinceCode"));
        paramMap.put("cityCode",jsonObject.getString("cityCode"));
        paramMap.put("districtCode",jsonObject.getString("districtCode"));
        paramMap.put("address",jsonObject.getString("address"));
        paramMap.put("logodata",jsonObject.getString("logodata"));
        paramMap.put("intro",jsonObject.getString("intro"));
        System.out.println("运行到转换");
        JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/saveOffice");//修改
        if(null != respone && 20000 == respone.getIntValue("code")) {
            return true;
        } else {
            throw new YyghException(respone.getString("message"), 201);
        }




    }

    @Override
    public boolean saveLawer(String data) {
        JSONArray jsonArray = new JSONArray();
        if(!data.startsWith("[")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            jsonArray.add(jsonObject);
        } else {
            jsonArray = JSONArray.parseArray(data);
        }

        for(int i=0, len=jsonArray.size(); i<len; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("officecode",jsonObject.getString("officecode"));
            paramMap.put("title",jsonObject.getString("title"));
            paramMap.put("lawername",jsonObject.getString("lawername"));
            paramMap.put("intro",jsonObject.getString("intro"));
            paramMap.put("worktime", jsonObject.getString("worktime"));
            paramMap.put("lawercode",jsonObject.getString("lawercode"));
            paramMap.put("skill",jsonObject.getString("skill"));
            paramMap.put("add",jsonObject.getString("add"));


          //  paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
          //  paramMap.put("sign",MD5.encrypt(this.getSignKey()));
            JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/saveLawer");
            System.out.println(respone.toJSONString());

            if(null == respone || 20000 != respone.getIntValue("code")) {
                throw new YyghException(respone.getString("message"), 201);
            }
        }
        return true;

    }

    @Override
    public Map<String, Object> findLawer(int pageNum, int pageSize) {
        Map<String, Object> result = new HashMap();

        Map<String, Object> paramMap = new HashMap<>();
        //paramMap.put("hoscode",this.getHoscode());
        //paramMap.put("depcode",depcode);
        paramMap.put("page",pageNum);
        paramMap.put("limit",pageSize);
      //  paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
       // paramMap.put("sign", HttpRequestHelper.getSign(paramMap, this.getSignKey()));
        //请求接口
        JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/lawer/list");
        if(null != respone && 20000 == respone.getIntValue("code")) {
            JSONObject jsonObject = respone.getJSONObject("data");

            result.put("total", jsonObject.getLong("totalElements"));
            result.put("pageNum", pageNum);
            result.put("list", jsonObject.getJSONArray("content"));
        } else {
            throw new YyghException(respone.getString("message"), 201);
        }
        return result;

    }

    @Override
    public boolean saveProfession(String data) {
        JSONArray jsonArray = new JSONArray();
        if(!data.startsWith("[")) {
            JSONObject jsonObject = JSONObject.parseObject(data);
            jsonArray.add(jsonObject);
        } else {
            jsonArray = JSONArray.parseArray(data);
        }

        for(int i=0, len=jsonArray.size(); i<len; i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("lawercode",jsonObject.getString("lawercode"));
            paramMap.put("professioncode",jsonObject.getString("professioncode"));
            paramMap.put("professionname",jsonObject.getString("professionname"));


            JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/saveProfession");
            System.out.println(respone.toJSONString());
            if(null == respone || 20000 != respone.getIntValue("code")) {
                throw new YyghException(respone.getString("message"), 201);
            }
        }
        return false;
    }

    @Override
    public boolean removeLawer(String lawercode,String officecode) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("officecode",officecode);
        paramMap.put("lawercode",lawercode);
        //paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
        //paramMap.put("sign", HttpRequestHelper.getSign(paramMap, this.getSignKey()));
        JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/lawer/remove");
        System.out.println(respone.toJSONString());
        if(null != respone && 20000 == respone.getIntValue("code")) {
            return true;
        } else {
            throw new YyghException(respone.getString("message"), 201);
        }
    }

    @Override
    public Map<String, Object> findProfession(int pageNum, int pageSize) {
        Map<String, Object> result = new HashMap();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page",pageNum);
        paramMap.put("limit",pageSize);
        JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/profession/list");
        if(null != respone && 20000 == respone.getIntValue("code")) {
            JSONObject jsonObject = respone.getJSONObject("data");

            result.put("total", jsonObject.getLong("totalElements"));
            result.put("pageNum", pageNum);
            result.put("list", jsonObject.getJSONArray("content"));
        } else {
            throw new YyghException(respone.getString("message"), 201);
        }
        return result;
    }

    @Override
    public boolean removeProfession(String professioncode, String lawercode) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("professioncode",professioncode);
        paramMap.put("lawercode",lawercode);
        //paramMap.put("timestamp", HttpRequestHelper.getTimestamp());
        //paramMap.put("sign", HttpRequestHelper.getSign(paramMap, this.getSignKey()));
        JSONObject respone = HttpRequestHelper.sendRequest(paramMap,"http://localhost:8888/api/office/profession/remove");
        System.out.println(respone.toJSONString());
        if(null != respone && 20000 == respone.getIntValue("code")) {
            return true;
        } else {
            throw new YyghException(respone.getString("message"), 201);
        }
    }


}
